/**
 * KAYO APPLIANCE CHAINS.
 * Copyright (c) 2016-2016 All Rights Reserved.
 */
package com.leku.video.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.leku.commons.util.StringUtil;
import com.leku.video.constants.AppConstant;
import com.leku.video.constants.ResultEnum;
import com.leku.video.dto.result.ResultDto;
import com.leku.video.util.ServletUtil;

/**
 * 功能描述: 签名验证
 * 
 * @author suiying
 * @created 2017年11月17日
 * @version 1.0.0
 */
public class SignValidFilter implements Filter {

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#destroy()
	 */
	@Override
	public void destroy() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
	 * javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;

		String sign = request.getHeader("sign");
		String nt = request.getHeader("time");
		String debug = request.getHeader("xxx");

		// 非debug模式需要校验签名
		if (!AppConstant.DEBUG.equals(debug)) {
			// 签名和时间为空则签名校验不通过
			if (StringUtil.isEmpty(sign) || StringUtil.isEmpty(nt)) {
				writeReponse(request, response);
				return;
			}

			// 签名校验不通过
			if (!sign.equals(geneSign(request))) {
				writeReponse(request, response);
				return;
			}
		}

		chain.doFilter(req, resp);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	@Override
	public void init(FilterConfig arg0) throws ServletException {
	}

	/**
	 * 
	 * 功能描述: 数据写入响应
	 *
	 * @param request
	 * @param response
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	private void writeReponse(HttpServletRequest request, HttpServletResponse response) {
		ResultDto<String> resultDto = new ResultDto<String>();
		resultDto.setRet(ResultEnum.SIGNERROR.getResCode());
		resultDto.setMsg(ResultEnum.SIGNERROR.getResMsg());

		ServletUtil.writeResponse(response, JSON.toJSONString(resultDto));
	}

	/**
	 * 
	 * 功能描述: 计算签名
	 *
	 * @param request
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	private String geneSign(HttpServletRequest request) {

		StringBuilder sb = new StringBuilder();
		sb.append("leku").append(request.getHeader("time")).append(request.getHeader("app"));

		if (StringUtil.isNotEmpty(request.getHeader("usr"))) {
			sb.append(request.getHeader("usr"));
		}

		sb.append(request.getHeader("ver")).append("yingshi").append(request.getHeader("os"));

		return StringUtil.str2MD5Of32(sb.toString());
	}
}
